import { defineStore } from 'pinia'
import { getMenuListApi } from '@/api/app'
import { getShowMenuList } from '@/utils/index'

import type { AuthState } from '@/store/types'

export const useAuthStore = defineStore('auth', () => {
	// 按钮权限列表
	const buttonList = ref<AuthState['buttonList']>({})
	// 菜单列表(未处理)
	const menuList = ref<AuthState['menuList']>([])
	// 当前页面的 router name，用来做按钮权限筛选
	const routeName = ref<AuthState['routeName']>('')
	// 左侧菜单栏渲染， 剔除 isHide == true
	const showMenuList = computed(() => getShowMenuList(menuList.value))
	// 递归处理后的所有面包屑导航列表
	// breadcrumbListGet: (state) => getAllBreadcrumbList(state.menuList),
	// 获取菜单树
	const getMenuList = async () => {
		const { data } = await getMenuListApi()
		console.log('获取菜单树：', data)
		if (!Array.isArray(data)) return
		menuList.value = data
	}
	const setRouteName = async (name: string) => {
		routeName.value = name
	}
	return {
		buttonList,
		menuList,
		routeName,
		showMenuList,
		getMenuList,
		setRouteName,
	}
})
